package com.hjm.project.terminal.manufacturer.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.common.utils.yh.Utils;
import com.hjm.project.terminal.manufacturer.domain.Manufacturer;
import com.hjm.project.terminal.manufacturer.mapper.ManufacturerMapper;
import com.hjm.project.terminal.manufacturer.service.IManufacturerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.common.utils.text.Convert;

/**
 * 厂商Service业务层处理
 * 
 * @author LOVE
 * @date 2020-11-26
 */
@Service
public class ManufacturerServiceImpl implements IManufacturerService
{
    @Autowired
    private ManufacturerMapper manufacturerMapper;

    /**
     * 查询厂商
     * 
     * @param manuNum 厂商ID
     * @return 厂商
     */
    @Override
    public Manufacturer selectManufacturerById(Long manuNum)
    {
        return manufacturerMapper.selectManufacturerById(manuNum);
    }

    /**
     * 查询厂商列表
     * 
     * @param manufacturer 厂商
     * @return 厂商
     */
    @Override
    public List<Manufacturer> selectManufacturerList(Manufacturer manufacturer)
    {
        return manufacturerMapper.selectManufacturerList(manufacturer);
    }

    /**
     * 新增厂商
     * 
     * @param manufacturer 厂商
     * @return 结果
     */
    @Override
    public Map<String,String> insertManufacturer(Manufacturer manufacturer) throws Exception {
        Map<String,String> result=new HashMap<>(16);
        result.put("code","00");
        result.put("msg","操作成功");
        try{
            int count = manufacturerMapper.getCountByCode(manufacturer.getManuCode());
            if (count>0){
                result.put("code","01");
                result.put("msg","厂家代号已存在");
                return result;
            }
            manufacturer.setWeight1(Utils.getWeight());
            manufacturer.setWeight2(Utils.getWeight());
            manufacturer.setResult(Utils.byte2HexStr(Utils.bytesXOR(Utils.hexStr2Bytes(manufacturer.getWeight1()),
                    Utils.hexStr2Bytes(manufacturer.getWeight2()))));
            manufacturer.setCheckval(Utils.byte2HexStr(Utils.encrypt(Utils.hexStr2Bytes("0000000000000000"),
                    Utils.hexStr2Bytes(manufacturer.getResult()))));
            manufacturer.setCreatename(ShiroUtils.getLoginName());
            manufacturer.setCreatetime(DateUtils.getNowDate());
            manufacturer.setObj_no(ShiroUtils.getLoginAgentNum());
            int i = manufacturerMapper.insertManufacturer(manufacturer);
            if (i <= 0) {
                result.put("code","01");
                result.put("msg","添加厂商失败");
                return result;
            }
        }catch (Exception e){
            e.printStackTrace();
            throw  e;
        }
        return result;
    }

    /**
     * 修改厂商
     * 
     * @param manufacturer 厂商
     * @return 结果
     */
    @Override
    public Map<String,String> updateManufacturer(Manufacturer manufacturer)
    {
        Map<String,String> result=new HashMap<>(16);
        result.put("code","00");
        result.put("msg","操作成功");
        manufacturerMapper.updateManufacturer(manufacturer);
        Manufacturer mm=selectManufacturerById(manufacturer.getManuNum());
        if (mm.getManuCode().equals(manufacturer.getManuCode())){
            result.put("code","01");
            result.put("msg","厂家代号已存在");
            return result;
        }
        return result;
    }

    /**
     * 删除厂商对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteManufacturerByIds(String ids)
    {
        return manufacturerMapper.deleteManufacturerByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除厂商信息
     * 
     * @param manuNum 厂商ID
     * @return 结果
     */
    @Override
    public int deleteManufacturerById(Long manuNum)
    {
        return manufacturerMapper.deleteManufacturerById(manuNum);
    }
    @Override
    public List<Map> getAll(Map map) {
        return manufacturerMapper.getAll(map);
    }

    @Override
    public List<Map> getManufacturer() throws Exception {
        return manufacturerMapper.getManufacturer();
    }
    @Override
    public List<Map> getManufacturer(String loginAgentNum) throws Exception {
        Manufacturer m = new Manufacturer();
        m.setLoginAgentNum(loginAgentNum);
        return manufacturerMapper.getManufacturer(m);
    }


    @Override
    public Map<String, Object> getActManufacturer(Map<String, Object> map) throws Exception {
        return manufacturerMapper.getActManufacturer(map).get(0);
    }


    @Override
    public List<Map<String, Object>> getTerminalModel(Map<String, Object> map) throws Exception {
        return manufacturerMapper.getTerminalModel(map);
    }

}
